/*
 * Copyright 2009 Jeremy Thomerson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/* VALIDATION OBJECTS: */
function AbstractValidator(field, fieldFriendlyName, message) {
	if (field != undefined) {
		this.field = field;
		this.fieldFriendlyName = fieldFriendlyName == null ? this.getBestGuessFriendlyName(field) : fieldFriendlyName;
	}
	this.message = message;
	this.errorMessages = new Array();
};

AbstractValidator.prototype.getBestGuessFriendlyName = function(field) {
	return field == null ? null : (field.name == null ? field.id : field.name);
}

AbstractValidator.prototype.validate = function() {
	console.debug('validator running: ' + typeof(this) + '; on field: ' + this.fieldFriendlyName);
	var jfield = $(this.field);
	var success = this.doValidation();
	console.debug('validator was ' + (success ? 'successful' : 'unsuccessful'));
	if (success) {
		this.onSuccess();
	} else {
		jVal.addErrorMessagesToElement(this.field, this.fieldFriendlyName, this.errorMessages);
		this.onFailure();
	}
	console.debug('resetting error messages on: ' + typeof(this) + '; on field: ' + this.fieldFriendlyName + '{was: ' + this.errorMessages + '}');
	this.errorMessages = new Array();
	return success;
};

AbstractValidator.prototype.onSuccess = function() {
	console.debug('triggering validator elemValidationSuccessful');
	$(this.field).trigger('elemValidationSuccessful');
};

AbstractValidator.prototype.onFailure = function() {
	console.debug('triggering validator elemValidationFailed');
	$(this.field).trigger('elemValidationFailed');
};

AbstractValidator.prototype.doValidation = function() {
	var success = this.isValid();
	if (!success) {
		this.addError(this.message);
	}
	return success;
};

AbstractValidator.prototype.addError = function(message) {
	this.errorMessages.push(message);
	return this;
};

AbstractValidator.prototype.getErrorMessages = function() {
	return this.errorMessages;
};


function AbstractStringValidator(field, fieldFriendlyName, message) {
	AbstractValidator.call(this, field, fieldFriendlyName, message);
}
AbstractStringValidator.prototype = new AbstractValidator();

AbstractStringValidator.prototype.isEmpty = function() {
	return this.field.value == null || this.field.value == '' || jQuery.trim(this.field.value) == '';
};

AbstractStringValidator.prototype.matchesPattern = function(str, pattern) {
	var regex = new RegExp(pattern, "g");
	return regex.test(str);
}


function RequiredValidator(field, fieldFriendlyName, message) {
	AbstractStringValidator.call(this, field, fieldFriendlyName, message);
	this.message = message;
}
RequiredValidator.prototype = new AbstractStringValidator();

RequiredValidator.prototype.isValid = function() {
	return this.isEmpty() == false;
};

function PatternValidator(field, fieldFriendlyName, message, pattern) {
	AbstractStringValidator.call(this, field, fieldFriendlyName, message);
	this.pattern = pattern;
}
PatternValidator.prototype = new AbstractStringValidator();

PatternValidator.prototype.isValid = function() {
	return this.matchesPattern(this.field.value, this.pattern);
};

function EmailAddressValidator(field, fieldFriendlyName, message) {
	this.pattern = '^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$';
	PatternValidator.call(this, field, fieldFriendlyName, message, this.pattern);
}
EmailAddressValidator.prototype = new PatternValidator();

